<?php
     /* bcms.inc.php Basic CMS.*/


class bcms {

    public $db;
    private $tbl_prefix = 'cms'; //This is the table name of the bcms table.

        //Our constructor.. it stores our connection in a var, if we have no sql connection, then we connect. :)
    function __construct() {
        global $db;
        if (!isset($db))
            $this->db = dbOn();
        else
            $this->db = $db;

        if(isset($this->db)) {
            debug('BCMS loaded.',0);
        } else {
            debug ('BCMS construct error',1);
        }
    }

    function getNodebyKey($node_key,$fresh = 0) {
        global $caTimeout;
        $db         = $this->db;
				$node_key   = str_replace("_"," ",$node_key);
        $safekey    = mysql_escape_string($node_key);
        //We want a case insensitive search...
        $query      = "SELECT * FROM `{$this->tbl_prefix}` WHERE `key`='{$safekey}' LIMIT 1;"; 

        $qkey       = mysql_escape_string(hash('crc32',$query));

        if($db->ifData($qkey)) {
            if($pg = $db->get_cache($qkey)) {
                return $pg[0];
            }
        } else {
            $res        = $db->query($query,$qkey,$fresh,$caTimeout); //We generally don't need the db calls to last long since we cache entire pages.
            if($pg = $db->get_cache($qkey)) {
                return $pg[0];
            } else {
                return;
            }
        }
    }
    function getNodebyId($node_id,$fresh = 0) {
        global $caTimeout;
        $db         = $this->db;

        $safeid    = mysql_escape_string($node_id);
        $query      = "SELECT * FROM `{$this->tbl_prefix}` WHERE `id`='{$safeid}' LIMIT 1;";

        $qkey       = mysql_escape_string(hash('crc32',$query));

        if($db->ifData($qkey)) {
            if($pg = $db->get_cache($qkey)) {
                return $pg[0];
            }
        } else {
            $res        = $db->query($query,$qkey,$fresh,$caTimeout); //We generally don't need the db calls to last long since we cache entire pages.
            if($pg = $db->get_cache($qkey)) {
                return $pg[0];
            } else {
                return;
            }
        }
    }

    function getNodeList($offset = 0, $limit = 25,$vis = 'all',$fresh = 0){
        global $caTimeout;

        $query = "SELECT `id`,`key`,`timestamp`,`auth_id`,`visible` FROM `{$this->tbl_prefix}`"; //The beginning of the call..

        $soffset = mysql_escape_string($offset);
        $slimit = mysql_escape_string($limit);

        switch ($vis) {
            case 'private':
                $query .= " WHERE `visible`='private'";
                break;
            case 'private':
                $query .= " WHERE `visible`='hidden'";
                break;
            case 'private':
                $query .= " WHERE `visible`='mgmt'";
                break;
        }
        $query .= " LIMIT $offset,$limit";

        $qkey       = mysql_escape_string(hash('crc32',$query)); //Lets make our query's unique key!
        $res = $this->queryCache($qkey,$query,$fresh,$caTimeout);

        return $res;
    }

    function getNodeCount() {
        global $caTimeout;
        $query = "SELECT count(id) FROM `{$this->tbl_prefix}`";

        $res = $this->queryCache('cmsCount',$query,0,350);
        return $res[0]['count(id)'];
    }
    
    function getVisArray() {
  		$db = $this->db;
			$res = $db->getEnumArray($this->tbl_prefix,'visible');
			return $res;
    }
    
    /*
        The $nodeData varible! 
    $nodeData['key']  = PageKey
    $nodeData['raw']  = Raw Unparsed Data
    $nodeData['html'] = XHTML parsed data.
    $nodeData['vis']  = Visibility of the object.
    $nodeData['authid'] = The ID of the author.
    $nodeData['seclev'] = The security level of the node, the seclev value must be equal
                          or less than the value of the authors security level.
    
    */
    function addNode($nodeData) {
			$db = $this->db;
    
  		foreach ($nodeData as $key => $value) {
  			$snodeData[$key] = mysql_escape_string($value);
  		}
  		
			$query = "INSERT INTO `{$this->tbl_prefix}` (
`id` ,`key` ,`raw` ,`html` ,`timestamp` ,`visible` ,`auth_id` ,`sec_level`) VALUES (
NULL , '{$snodeData['key']}', '{$snodeData['raw']}', '{$snodeData['html']}',
CURRENT_TIMESTAMP , '{$nodeData['vis']}', '{$nodeData['authid']}', '{$nodeData['seclev']}')";
			$qkey       = mysql_escape_string(hash('crc32',$query));
			//Now lets run the query..
			$res        = $db->query($query,$qkey,1,1); //Always fresh, and a timeout of 1ms
			return $res;
    }
    
    function updateNode($nodeData) {
			$db = $this->db;
    
  		foreach ($nodeData as $key => $value) {
  			$snodeData[$key] = mysql_escape_string($value);
  		}
//	    if(!isset($id)) return false; else $sID = mysql_escape_string($id);
    
  		$query = "UPDATE `{$this->tbl_prefix}` SET `raw` = '{$snodeData['raw']}',
`html` = '{$snodeData['html']}',
`visible` = '{$nodeData['vis']}',
`auth_id` = '{$nodeData['authid']}',
`sec_level` = '{$nodeData['seclev']}' WHERE `{$this->tbl_prefix}`.`key` = '{$snodeData['key']}' LIMIT 1 ;";
			$qkey       = mysql_escape_string(hash('crc32',$query));
			
			$res        = $db->query($query,$qkey,1,1); //Always fresh, and a timeout of 1ms
			return $res;
    }

    function queryCache($qkey,$query,$fresh,$timeout) {
        $db = $this->db;
        if($db->ifData($qkey)) {
            if($pg = $db->get_cache($qkey)) {
                return $pg;
            }
        } else {
            $res        = $db->query($query,$qkey,$fresh,$timeout); //We generally don't need the db calls to last long since we cache entire pages.
            if($pg = $db->get_cache($qkey)) {
                return $pg;
            } else {
                return;
            }
        }
    }

}

?>
